package com.aia.ilp.web.sanity;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

public class TestserverAction extends ActionSupport {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TestserverAction.class);

	/**
	 * Logger for this class
	 */

	private static final long serialVersionUID = 1L;

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html");
			response.setHeader("Content-Disposition", "attachment; filename=\"log_"
					+ System.currentTimeMillis() + ".txt\"");
			OutputStream os = response.getOutputStream();
			String root = System.getProperty(Tools.getProperties("system.rootkey"));
			if(root!=null && root.trim().length()>0){
				File file = new File(root+"/WEB-INF/log.txt");
				FileInputStream fis = new FileInputStream(file);
				IOUtils.copy(fis, os);
				IOUtils.closeQuietly(fis);
			}
			os.flush();
			IOUtils.closeQuietly(os);
		} catch (javax.imageio.IIOException iioe) {
			if (logger.isDebugEnabled()) {
				logger.debug("execute()"); //$NON-NLS-1$
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("imageAction error:", e);
			throw new RuntimeException(e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return null;
	}


}
